home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / pp / pp-6.0 / Tools / hex2bin / hex2bin.c next >
Encoding:
C/C++ Source or Header  |  1991-12-18  |  1.6 KB  |  103 lines

  1. /* hex2bin: Converts a hex file to binary */
  2.  
  3. # ifndef lint
  4. static char Rcsid[] = "@(#)$Header: /xtel/pp/pp-beta/Tools/hex2bin/RCS/hex2bin.c,v 6.0 1991/12/18 20:30:25 jpo Rel $";
  5. # endif
  6.  
  7. /*
  8.  * $Header: /xtel/pp/pp-beta/Tools/hex2bin/RCS/hex2bin.c,v 6.0 1991/12/18 20:30:25 jpo Rel $
  9.  *
  10.  * $Log: hex2bin.c,v $
  11.  * Revision 6.0  1991/12/18  20:30:25  jpo
  12.  * Release 6.0
  13.  *
  14.  */
  15.  
  16.  
  17.  
  18. #include <stdio.h>
  19.  
  20.  
  21.  
  22. /* ---------------------  Begin  Routines  -------------------------------- */
  23.  
  24.  
  25. main(argc, argv)
  26. int argc;
  27. char **argv;
  28. {
  29.     if (argc > 1) {
  30.         while (--argc > 0) {
  31.             FILE *fp;
  32.  
  33.             if ((fp = fopen (*++argv, "r")) == NULL) {
  34.                 fprintf (stderr, "Can't open file %s", *argv);
  35.                 perror("");
  36.                 exit (1);
  37.             }
  38.             hex2bin(fp);
  39.             (void) fclose (fp);
  40.         }
  41.     }
  42.     else
  43.         hex2bin (stdin);
  44.  
  45.  
  46. }
  47.  
  48. hex2bin (fp)
  49. FILE *fp;
  50. {
  51.     int zone, first_zone, octet;
  52.  
  53.     first_zone = 1;
  54.     octet = 0;
  55.     while ((zone = getc(fp)) != EOF)
  56.     {
  57.         switch (zone)
  58.         {
  59.             case '0' :
  60.             case '1' :
  61.             case '2' :
  62.             case '3' :
  63.             case '4' :
  64.             case '5' :
  65.             case '6' :
  66.             case '7' :
  67.             case '8' :
  68.             case '9' : zone -= '0';
  69.             break;
  70.             case 'a' :
  71.             case 'b' :
  72.             case 'c' :
  73.             case 'd' :
  74.             case 'e' :
  75.             case 'f' : zone = zone - 'a' + 10;
  76.             break;
  77.  
  78.             case 'A' :
  79.             case 'B' :
  80.             case 'C' :
  81.             case 'D' :
  82.             case 'E' :
  83.             case 'F' : zone = zone - 'A' + 10;
  84.             break;
  85.             default:
  86.             continue;
  87.         }
  88.         if (first_zone)
  89.         {
  90.             octet = zone * 16;
  91.             first_zone = 0;
  92.         }
  93.         else
  94.         {
  95.             octet += zone;
  96.             putchar(octet);
  97.             first_zone = 1;
  98.         }
  99.     }
  100.     if (!first_zone)
  101.         putchar(octet);
  102. }
  103.